/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//		File Name		:		CGamePlayState.h
//
//		Author			:		Vindicated Games
//
//		Purpose			:		A header for all functions and members of the Game Play State
//
//		Dates			:		Written( Feb 12, 2011 ) by Vindicated Games		LastUpdate( Feb 17, 2011  ) by Vindicated Games 
//	
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "CGamePlayState.h"
#include "../SGD Wrappers/CSGD_TextureManager.h"
#include "../SGD Wrappers/CSGD_DirectInput.h"
#include "../SGD Wrappers/CSGD_Direct3D.h"
#include "../Managers/VIN_ObjectManager.h"
#include "../Managers/VIN_SoundManager.h"
#include "../Game Objects/CMasterHand.h"
#include "../Game Objects/CMeleeEnemy.h"
#include "../SGD Wrappers/SGD_Math.h"
#include "../Visual/VIN_BitMapFont.h"
#include "../Game Objects/CPowerUp.h"
#include "../Core/VIN_EventSystem.h"
#include "../Game Objects/CPlayer.h"
#include "../Game Objects/CButton.h"
#include "../Game Objects/CEnemy.h"
#include "../Core/MessageSystem.h"
#include "../Game Objects/CRock.h"
#include "../tinyxml/tinyxml.h"
#include "CSaveOrLoadState.h"
#include "CHighScoreState.h"
#include "CMainMenuState.h"
#include "../Core/CGame.h"

#include "../Managers/InputManager.h"

#include <ctime>

// MOVE EVERYTHING FROM TESTING LEVEL STATE TO HERE
CGamePlayState::CGamePlayState()
{
	// Use static images from message system
	HUDButtonID = CSGD_TextureManager::GetInstance()->LoadTexture("resources/graphics/HUDButton.png");
	ImageID = -1;
	fBucketTE = 0.0f;
	VIN_EventSystem::GetInstance()->ResgisterClient("KillAll",this);
	WaveCount = 0;
	fEnemySpawnBucket = 0.0f;
	m_nNumOfEnemies = 0;
	m_nMaxEnemies = 0;
	m_nNumOfDeadEnemies = 0;
	m_fWaveTime = 100.0f;
	m_bNoTimeUpdate = false;
	m_bRoundComplete = false;
	m_nRoundCount = 1;
	m_fEndOfWaveBucket = 0;
	m_bWaveOver = false;
	pPlayer = NULL;

	/////////////////////////////////////////////////////////////////////

	HUDID = MessageSystem::GetInstance()->GetStaticImageID(IMAGE_PIXEL);
	ImageID = CSGD_TextureManager::GetInstance()->LoadTexture("resources/graphics/background.png");
	castleID = CSGD_TextureManager::GetInstance()->LoadTexture("resources/graphics/castle.png");

	m_bTriggedEvent = false;
	m_bGamePaused = false;
	IsInGame = false;
	PauseID = 0;
	m_nPauseSelection = 0;
	score = 0;
	pPlayer = NULL;
}

CGamePlayState::~CGamePlayState()
{
	VIN_EventSystem::GetInstance()->UnregisterClient("KillAll",this);
}

void CGamePlayState::Enter()
{
	background = CSGD_TextureManager::GetInstance()->LoadTexture("resources/Graphics/scroll.png");
	back = CSGD_TextureManager::GetInstance()->LoadTexture("resources/Graphics/background.png");
	castle = CSGD_TextureManager::GetInstance()->LoadTexture("resources/Graphics/castle.png");
	VIN_ObjectManager* pOM = VIN_ObjectManager::GetInstance();
	VIN_EventSystem::GetInstance()->ResgisterClient("DynTrig",this);
	start = true;
	/////////////////////////////////////////////////////////////////////

	m_bGamePaused = false;
	m_bNoTimeUpdate = false;
	m_vWaves.clear();
	InitWaves();

	VIN_SoundManager::GetInstance()->PlayBGM(BGM_STAGE);
	Load("resources/XML/LionO.xml");
	/////////////////////////////////////////////////////////////////////

	//Instantiate a new Player
	pPlayer = new CPlayer();
	pPlayer->SetImageID(MessageSystem::GetInstance()->GetStaticImageID(IMAGE_CASTLE1));
	pPlayer->ResetPlayer();

	masterHand = new CMasterHand();

	pOM->AddObject(2, masterHand);

	pOM->SetPlayer(pPlayer);

	/////////////////////////////////////////////////////
	/////////////////////////////////////////////////////
	//  HUD Buttons
	CButton* HandButton;
	CButton* RockButton;
	CButton* PoisonButton;
	CButton* OilButton;
	CButton* BallistaButton;
	CButton* ArcherButton;
	CButton* CastleButton;

	/////////////////////////////////////////////////////////////////////
	HandButton = new CButton();
	HandButton->SetUpRect(290,10,360,75);
	HandButton->SetButtonType(BUTTON_HAND);
	HandButton->SetItemSize(0.75f);
	HandButton->SetButtonColor(255, 128, 128,128);
	HandButton->SetItemID(MessageSystem::GetInstance()->GetStaticImageID(IMAGE_HAND));
	HandButton->SetImageID(HUDButtonID);
	pOM->AddObject(2, HandButton);
	HandButton->Release();

	RockButton = new CButton();
	RockButton->SetUpRect(450, 10, 520, 75);
	RockButton->SetButtonType(BUTTON_ROCK);
	RockButton->SetItemSize(0.35f);
	RockButton->SetButtonColor(255, 128, 128,255);
	RockButton->SetItemID(MessageSystem::GetInstance()->GetStaticImageID(IMAGE_ROCK));
	RockButton->SetImageID(HUDButtonID);
	RockButton->SetPrice(250 * GetHand()->GetRockLevel());
	pOM->AddObject(2, RockButton);
	RockButton->Release();

	PoisonButton = new CButton();
	PoisonButton->SetUpRect(370,10,440,75);
	PoisonButton->SetButtonType(BUTTON_POISON);
	PoisonButton->SetItemSize(.75f);
	PoisonButton->SetButtonColor(255, 255, 0,255);
	PoisonButton->SetItemID(MessageSystem::GetInstance()->GetStaticImageID(IMAGE_KOFFING));
	PoisonButton->SetImageID(HUDButtonID);
	pOM->AddObject(2, PoisonButton);
	PoisonButton->Release();

	OilButton = new CButton();
	OilButton->SetUpRect( 530, 10, 600,75);
	OilButton->SetButtonType(BUTTON_OIL);
	OilButton->SetItemSize(.80f);
	OilButton->SetButtonColor(255, 255, 255,128);
	OilButton->SetItemID(MessageSystem::GetInstance()->GetStaticImageID(IMAGE_POT));
	OilButton->SetImageID(HUDButtonID);
	OilButton->SetPrice(300);
	pOM->AddObject(2, OilButton);
	OilButton->Release();

	BallistaButton = new CButton();
	BallistaButton->SetUpRect(610, 10, 680,75);
	BallistaButton->SetButtonType(BUTTON_BALLISTA);
	BallistaButton->SetItemSize(.80f);
	BallistaButton->SetButtonColor(255, 0, 255, 255);
	BallistaButton->SetItemID(MessageSystem::GetInstance()->GetStaticImageID(IMAGE_BALLISTA));
	BallistaButton->SetImageID(HUDButtonID);
	BallistaButton->SetPrice(300);
	pOM->AddObject(2, BallistaButton);
	BallistaButton->Release();

	ArcherButton = new CButton();
	ArcherButton->SetUpRect(690, 10,760,75);
	ArcherButton->SetButtonType(BUTTON_ARCHER);
	ArcherButton->SetItemSize( .75f);
	ArcherButton->SetButtonColor(255, 255, 0,0);
	ArcherButton->SetItemID(MessageSystem::GetInstance()->GetStaticImageID(IMAGE_BOW));
	ArcherButton->SetImageID(HUDButtonID);
	ArcherButton->SetPrice(100);
	pOM->AddObject(2, ArcherButton);
	ArcherButton->Release();

	CastleButton = new CButton();
	CastleButton->SetUpRect((int)pPlayer->GetPosX(), (int)pPlayer->GetPosY(),(int)(pPlayer->GetPosX() + pPlayer->GetWidth()), (int)(pPlayer->GetPosY() + pPlayer->GetHeight()));
	CastleButton->SetButtonType(BUTTON_CASTLEUPGRADE);
	CastleButton->SetItemSize( 0.0f);
	CastleButton->SetButtonColor(0, 0, 0,0);
	CastleButton->SetItemID(MessageSystem::GetInstance()->GetStaticImageID(IMAGE_ROCK));
	CastleButton->SetImageID(-1);
	CastleButton->SetPrice(1500);
	pOM->AddObject(2, CastleButton);
	CastleButton->Release();

	/////////////////////////////////////////////////////////////////////
}

void CGamePlayState::Exit() 
{
	CSGD_TextureManager::GetInstance()->UnloadTexture(background);
	CSGD_TextureManager::GetInstance()->UnloadTexture(back);
	CSGD_TextureManager::GetInstance()->UnloadTexture(castle);
	m_bTriggedEvent = false;
	VIN_EventSystem::GetInstance()->UnregisterClient("DynTrig",this);

	//pLevelState->Exit();
	/////////////////////////////////////////////////////////////////////
	// VIN_SoundManager::GetInstance()->StopAllSounds();

	VIN_ObjectManager::GetInstance()->RemoveAllObjects();
	GetPlayer()->ResetPlayer();
	GameOver(false);
	WinGame(false);
	WaveCount = 0;
	m_nRoundCount = 1;
	//////////////////////////////////////////////////////////////

	IsInGame = false;
	m_bWinGame = false;
	m_bGameOver = false;
	m_bGamePaused = false;

	pPlayer->Release();
	masterHand->Release();
}

CGamePlayState* CGamePlayState::GetInstance()
{
	static CGamePlayState instance;
	return &instance;
}

void CGamePlayState::Render()
{
	CSGD_Direct3D* pD3D = CSGD_Direct3D::GetInstance();
	VIN_BitMapFont* pBF = VIN_BitMapFont::GetInstance();
	CSGD_TextureManager* pTM = CSGD_TextureManager::GetInstance();
	pD3D->Clear(0,0,0);
	if( !m_bGamePaused )
	{
		pTM->Draw(ImageID, 0,0);

		if(GetTrigger())
		{
			VIN_BitMapFont::GetInstance()->Render("Triggered Event", (int)(GetPlayer()->GetPosX() - 55), (int)(GetPlayer()->GetPosY() - 55), 1, D3DCOLOR_ARGB(0,255,0,0), 1);
		}

		if( GetWin() == false && GetLose() == false )
		{
			RenderHUD();
		}
		pD3D->GetSprite()->Flush();
		//////////////////////////////////////////////////////////////

		VIN_ObjectManager::GetInstance()->RenderObjects();

		if(m_bWinGame)
		{
			
			VIN_ObjectManager::GetInstance()->RemoveAllObjects();
			VIN_SoundManager::GetInstance()->StopSound(VIN_SoundManager::GetInstance()->GetAlarm());
	VIN_SoundManager::GetInstance()->StopAllSounds();
			pTM->Draw(back, 0, 0);
			pTM->Draw(castle, 500,160, 1.5f, 1.5f);
			pTM->Draw(background, 0, -1800, 2.0f, 2.0f);
			pBF->Render("You WIN!!!", 220,100,2.0f, D3DCOLOR_ARGB(255,0,0,0),MENUFONT);
			vAnimation[0].Render(600,220);
//			char timebuff[100];
			// TODO: put in Variable
			//pBF->Render(timebuff, 250,250,.75f, D3DCOLOR_ARGB(255,0,0,0),GAMEFONT);

			char scorebuff[100];
			// TODO: put in Variable
			sprintf_s(scorebuff, 100, "Total Score :%i ", score);
			pBF->Render(scorebuff, 250,300,.75f, D3DCOLOR_ARGB(255,0,0,0),GAMEFONT);
	
		}
		if(m_bGameOver)
		{
			VIN_ObjectManager::GetInstance()->RemoveAllObjects();
			VIN_SoundManager::GetInstance()->StopSound(VIN_SoundManager::GetInstance()->GetAlarm());
	
			pTM->Draw(back, 0, 0);
			pTM->Draw(castle, 500,160, 1.5f, 1.5f);
			pTM->Draw(background, 0, -1800, 2.0f, 2.0f);
			pBF->Render("GAME OVER!!!", 220,100,1.75f, D3DCOLOR_ARGB(255,0,0,0),MENUFONT);

			char scorebuff[100];
			//TODO: put in Variable
			sprintf_s(scorebuff, 100, "Total Score : %i", score);
			pBF->Render(scorebuff, 250,300,.75f, D3DCOLOR_ARGB(255,0,0,0),GAMEFONT);
		}
	}
	else if( m_bGamePaused )
	{
		//VIN_SoundManager::GetInstance()->StopSound(VIN_SoundManager::GetInstance()->GetAlarm());
		
		pTM->Draw( PauseID, 0,0,1.0f,1.0f,0,0.0f,0.0f,0.0f, D3DCOLOR_ARGB(128,0,0,0));
		pBF->Render( "PAUSE", 300, 200, 2, D3DCOLOR_ARGB(255,255,0,0), GAMEFONT);

		pBF->Render( "RESUME", 350, 255, 1, D3DCOLOR_ARGB(255,255,0,0), MENUFONT);
		pBF->Render( "QUIT LEVEL", 350, 300, 1, D3DCOLOR_ARGB(255,255,0,0), MENUFONT);

		pTM->Draw( CMainMenuState::GetInstance()->GetCursorID(), 300,(260 + (45 * m_nPauseSelection)),2.0f,1.0f,0,0.0f,0.0f,0.0f, D3DCOLOR_ARGB(255,255,0,0));
	}
}

bool CGamePlayState::Input()
{
	CSGD_DirectInput*	pDI = CSGD_DirectInput::GetInstance();
	CGame*				pGameState = CGame::GetInstance();

	if( !m_bGamePaused )
	{	
		VIN_ObjectManager::GetInstance()->InputObjects();

		//pLevelState->Input();
		//////////////////////////////////////////////////////////////
		if(GetWin() == false && GetLose() == false)
		{
			if( InputManager::GetInstance()->Escape() ) 
				SetPause( !GetPause() );
		}
		//////////////////////////////////////////////////////////////
	}

	if( m_bGamePaused )
	{	
		if( InputManager::GetInstance()->Escape() )
		{
			//m_bGamePaused = !m_bGamePaused;
		}

		if(InputManager::GetInstance()->MoveUp())
		{	
			VIN_SoundManager::GetInstance()->PlayMenuSelect();

			m_nPauseSelection--;
			if( m_nPauseSelection < 0 )
				m_nPauseSelection = 1;
		}
		else if( InputManager::GetInstance()->MoveDown())
		{
			VIN_SoundManager::GetInstance()->PlayMenuSelect();

			m_nPauseSelection++;
			if( m_nPauseSelection > 1 )
				m_nPauseSelection = 0;		
		}
		if( InputManager::GetInstance()->Enter())
		{
				VIN_SoundManager::GetInstance()->PlayMenuConfirm();

			switch( m_nPauseSelection )
			{
			case 0:
				{
					m_bGamePaused = false;				
					m_nPauseSelection = 0;
				}
				break;
			case 1:
				{
					m_bGamePaused = false;
					m_nPauseSelection = 0;
					CGame::GetInstance()->ChangeState(CMainMenuState::GetInstance());
			CMainMenuState::GetInstance()->main_scrolling2 = true;
				}
				break;
			}
		}
	}
	return true;
}

void CGamePlayState::Update(float fElapsedTime)
{
	
	if(start)
	{
		VIN_SoundManager::GetInstance()->PlayStart();
		VIN_SoundManager::GetInstance()->PlayStart();
		VIN_SoundManager::GetInstance()->PlayStart();
		start = false;
	}
	CSGD_DirectInput* pDI = CSGD_DirectInput::GetInstance();
	if( !m_bGamePaused )
	{
		//GetHand()->GetCollisionRect()
		//levelstate->update();
		//////////////////////////////////////////////////////////////

		m_JCE.Update(fElapsedTime);
		if( GetTrigger() )
		{
			fBucketTE += fElapsedTime;
			if( fBucketTE >= 3.5f )
			{
				SetTrigger(false);
				fBucketTE = 0.0f;
			}
		}

		fEnemySpawnBucket += fElapsedTime;
		if( fEnemySpawnBucket >= 1.5f )
		{
			if( m_nNumOfEnemies < m_nMaxEnemies )
			{
				SpawnEnemies();
				fEnemySpawnBucket = 0.0f;
			}
		}

		if(m_nNumOfDeadEnemies == m_nMaxEnemies && !m_bWaveOver )
		{
			m_bWaveOver = true;
			VIN_SoundManager::GetInstance()->PlayStart();
		}

		if( m_bWaveOver == true )
		{
			m_fEndOfWaveBucket += fElapsedTime;
			if( m_fEndOfWaveBucket >= 1.75f )
			{
				int tempRand = rand()%20;
				if(tempRand < 4)
				{
					MessageSystem::GetInstance()->SendNewMessage(new CCreateParticleEmitterMessage(400.0f, -10.0f, 0.0f, 0.0f, 2));
				}
				else if( tempRand > 14 )
				{
					MessageSystem::GetInstance()->SendNewMessage(new CCreateParticleEmitterMessage(400.0f, 0.0f, 0.0f, 0.0f, 3));
				}

				WaveSet();
				m_bWaveOver = false;
				m_fEndOfWaveBucket = 0.0f;
			}
		}
		

		if((m_JCE.GetPosX() + m_JCE.GetWidth()) > 800)
		{
			m_JCE.SetPosX(700);
		}
		if(m_JCE.GetPosX() - m_JCE.GetWidth() < 0)
		{
			m_JCE.SetPosX(0);
		}
		if(m_JCE.GetPosY() + m_JCE.GetHeight() > 600)
		{
			m_JCE.SetPosY(500);
		}

		VIN_EventSystem::GetInstance()->ProcessEvents();
		VIN_ObjectManager::GetInstance()->CheckCollision();
		VIN_ObjectManager::GetInstance()->UpdateObjects(fElapsedTime);
		//pEmitter->Update(fElapsedTime);

		if( !m_bNoTimeUpdate )
		{
			m_fWaveTime -= fElapsedTime;
		}

		if( GetPlayer()->GetHP() <= 0 )
		{
			GameOver(true);
			CalculateScore();
			VIN_SoundManager::GetInstance()->StopSound(VIN_SoundManager::GetInstance()->GetAlarm());
			m_bNoTimeUpdate = true;
		}
		else if(GetWaveCount() == 10 && m_bWaveOver == true && m_nMaxEnemies == m_nNumOfDeadEnemies)
		{
			WinGame(true);
			CalculateScore();
			VIN_SoundManager::GetInstance()->StopSound(VIN_SoundManager::GetInstance()->GetAlarm());
			m_bNoTimeUpdate = true;
		}

		else if(m_fWaveTime <= 5.0f)
		{
			VIN_SoundManager::GetInstance()->PlayAlarm();
		}
		else
			VIN_SoundManager::GetInstance()->StopSound(VIN_SoundManager::GetInstance()->GetAlarm());

	/*	if( m_bRoundComplete && m_nRoundCount <= 3)
		{
			ResetRound();
			InitWaves();
			WaveSet();
		}
		else if( m_nRoundCount == 3 && m_nNumOfDeadEnemies == m_nMaxEnemies  && WaveCount == 5)
		{
			CalculateScore();
			WinGame(true);
			m_bNoTimeUpdate = true;
		}*/
		//////////////////////////////////////////////////////////////

		VIN_EventSystem::GetInstance()->ProcessEvents();

		static float ScreenTime = 0;

		if(GetWin())
		{
			if(ScreenTime == 0.0f)
				VIN_SoundManager::GetInstance()->PlayWin();
			ScreenTime += fElapsedTime;
			vAnimation[0].Update(fElapsedTime * 5);
			vAnimation[0].Play();
			if(vAnimation[0].GetCurFrame() >= vAnimation[0].GetMaxFrames() - 1)
				vAnimation[0].Stop();
			if(ScreenTime > 10 || pDI->KeyPressed( DIK_ESCAPE ) || pDI->KeyPressed( DIK_RETURN ))
			{
				m_bGameOver = m_bWinGame = false;
				ScreenTime = 0;
				CalculateScore();
				
				if(CHighScoreState::GetInstance()->LowestScore() < score )
				{
					CHighScoreState::GetInstance()->Achievement(true, score);
					score = 0;
				}
				CGame::GetInstance()->ChangeState(CHighScoreState::GetInstance());
			}
		}

		if(GetLose())
		{
			if(ScreenTime == 0.0f)
				VIN_SoundManager::GetInstance()->PlayGameOver();

			ScreenTime += fElapsedTime;
			if(ScreenTime > 10 || pDI->KeyPressed( DIK_ESCAPE ) || pDI->KeyPressed( DIK_RETURN ))
			{
				m_bGameOver = m_bWinGame = false;
				ScreenTime = 0;
				CalculateScore();
				if(CHighScoreState::GetInstance()->LowestScore() < score )
				{
					CHighScoreState::GetInstance()->Achievement(true, score);
					score = 0;
				}
				CGame::GetInstance()->ChangeState(CHighScoreState::GetInstance());
			}
		}
	}

	MessageSystem::GetInstance()->ProcessMessages();
}

void CGamePlayState::UpdatePlayerProfile()
{
	/*CSaveOrLoadState::GetInstance()->FillProfile( CGame::GetInstance()->SaveSlot, ActiveProfile.GetLevel(), 
	ActiveProfile.GetHealth(), ActiveProfile.SetDurability(), ActiveProfile.SetCombat, ActiveProfile.SetSpeed(), ActiveProfile.SetSpeed() );*/
}

void CGamePlayState::HandleEvent(CEvent* pEvent)
{	
	if( pEvent->GetEventID() == "DynTrig" )
	{
		if( pEvent->GetParam() == this )
			m_bTriggedEvent = true;
	}

	if(pEvent->GetEventID() == "KillAll")
	{
		VIN_ObjectManager::GetInstance()->KillAll();
		m_nNumOfDeadEnemies = m_nNumOfEnemies;
	}
}

void CGamePlayState::RenderHUD()
{
	CSGD_Direct3D* pD3D = CSGD_Direct3D::GetInstance();
	CSGD_TextureManager* pTM = CSGD_TextureManager::GetInstance();
	VIN_BitMapFont* pBF = VIN_BitMapFont::GetInstance();

	// HUD BackGround
	pTM->Draw(HUDID, 25,0,(float)pD3D->GetPresentParams()->BackBufferWidth - 50, 100.0f, 0,0,0,0, D3DCOLOR_ARGB(180,255,255,255));

	// HP && Health Bar
	pBF->Render("HP:",35,10, 0.75, D3DCOLOR_ARGB(255,0,0,0), GAMEFONT);
	pTM->Draw(HUDID, 85,10, 200, 20.0f, 0,0,0,0, D3DCOLOR_ARGB(255,255,0,0));
	if(pPlayer->GetHP() >= 0)
		pTM->Draw(HUDID, 85,10,((float)pPlayer->GetHP()/(float)pPlayer->GetMaxHealth()) * 200, 20.0f, 0,0,0,0, D3DCOLOR_ARGB(255,0,255,0));

	// XP Bar
	char Lvl[32];
	sprintf_s(Lvl, "LvL: %d", GetHand()->GetLevel());
	pBF->Render(Lvl,35,35, 0.75, 0.5f, D3DCOLOR_ARGB(255,0,0,0), GAMEFONT);
	pBF->Render("XP",140,35, 0.75, 0.5f, D3DCOLOR_ARGB(255,0,0,0), GAMEFONT);
	pTM->Draw(HUDID, 185,33, 100, 15.0f, 0,0,0,0, D3DCOLOR_ARGB(255,0,255,0));
	if(GetHand()->GetXP() >= 0)
		pTM->Draw(HUDID, 185,35, (float)(GetHand()->GetXP()/GetHand()->GetNeededXP()) * 100.0f, 10.0f, 0,0,0,0, D3DCOLOR_ARGB(255,100,128,255));

	// Enemy Count and Wave count
	char EnemyCount[128];
	char WaveCount[32];
	sprintf_s(EnemyCount, "Enemies Left: %d", GetMaxEnemies() - GetDeadEnemies());
	sprintf_s(WaveCount, "Wave: %d", GetWaveCount());
	pBF->Render(WaveCount,35,75, 0.75, D3DCOLOR_ARGB(255,0,0,0), GAMEFONT);
	pBF->Render(EnemyCount,35,50, 0.75, D3DCOLOR_ARGB(255,0,0,0), GAMEFONT);
	// Money!!!
	char Moneys[64];
	sprintf_s(Moneys, "$$$: %d", GetPlayer()->GetCash());
	pBF->Render(Moneys, 140, 75, 0.75, D3DCOLOR_ARGB(255,0,0,0), GAMEFONT);

}
int CGamePlayState::CalculateScore()
{
	// rounds(100) + money + upgrades
	score = (GetRoundCount() * GetRoundCount() * 10) + (GetPlayer()->GetCash() * 50) + (GetHand()->GetLevel() * 100);
	return score;
}

void CGamePlayState::WaveSet()
{

	if( WaveCount <  10)
	{	
		//HEROBRINE WAS HERE. . . . . Surfing on the Wave from Vector below
		//Load in the Wave from the Vector
		
		WaveInfo tempWave;

		int Size = m_vWaves.size();

		if(Size > 0)
		{
			tempWave.fWaveTime = m_vWaves[(Size - 1)].fWaveTime;
			tempWave.NumOfEnemies = m_vWaves[(Size - 1)].NumOfEnemies;

			m_vWaves.pop_back();
			//Increment the Wave count
			WaveCount++;
			//Set the Max Num of Enemies
			m_nMaxEnemies = tempWave.NumOfEnemies;
			//Reset the Number of dead and  alive enemies
			m_nNumOfEnemies = 0;
			m_nNumOfDeadEnemies = 0;
			m_fWaveTime = tempWave.fWaveTime;
		}
	}
	else if( m_nRoundCount < 3 )
	{
		m_bNoTimeUpdate = true;
		m_bRoundComplete = true;
		WaveCount = 0;
		m_nRoundCount++;
	}

}

void CGamePlayState::SpawnEnemies()
{
	int enemyType = rand() % 10;

	int spawnType = rand() % 10;

	if(GetWaveCount() < 4)
	{
		if(enemyType >= 2)
		{
			MessageSystem::GetInstance()->SendNewMessage(new CCreateRangedEnemyMessage(-60, 575, 0, GetWaveCount()));
		}
		else if(enemyType <= 1)
		{
			if(spawnType <= 6)
				MessageSystem::GetInstance()->SendNewMessage(new CCreateMeleeEnemyMessage(-50, 575, 0, 1));
			else if(spawnType == 8 || spawnType == 7)
				MessageSystem::GetInstance()->SendNewMessage(new CCreateMeleeEnemyMessage(-50, 575, 1, 1));
			else if(spawnType == 9)	
				MessageSystem::GetInstance()->SendNewMessage(new CCreateMeleeEnemyMessage(-50, 575, 2, 1));
		}
	}
	else if(GetWaveCount() >= 4 && GetWaveCount() < 7)
	{
		if(enemyType >= 4)
		{
			if(spawnType < 7)
				MessageSystem::GetInstance()->SendNewMessage(new CCreateRangedEnemyMessage(-60, 575, 0, GetWaveCount()));
			else if(spawnType == 7)
				MessageSystem::GetInstance()->SendNewMessage(new CCreateExplodingEnemyMessage(-50, 575, GetWaveCount()));
			else if(spawnType == 8)
				MessageSystem::GetInstance()->SendNewMessage(new CCreateRangedEnemyMessage(-50, 575, 2, GetWaveCount()));
			else if(spawnType == 9)
				MessageSystem::GetInstance()->SendNewMessage(new CCreateRangedEnemyMessage(-50, 575, 3, GetWaveCount()));
		}
		else if(enemyType <= 3)
		{
			if(spawnType <= 6)
				MessageSystem::GetInstance()->SendNewMessage(new CCreateMeleeEnemyMessage(-50, 575, 0, 1));
			else if(spawnType == 8 || spawnType == 7)
				MessageSystem::GetInstance()->SendNewMessage(new CCreateMeleeEnemyMessage(-50, 575, 1, 1));
			else if(spawnType == 9)	
				MessageSystem::GetInstance()->SendNewMessage(new CCreateMeleeEnemyMessage(-50, 575, 2, 1));
		}
	}
	else
	{
		if(enemyType >= 5)
		{
			if(spawnType < 5)
				MessageSystem::GetInstance()->SendNewMessage(new CCreateRangedEnemyMessage(-60, 575, 0, GetWaveCount()));
			else if(spawnType == 5 )
				MessageSystem::GetInstance()->SendNewMessage(new CCreateExplodingEnemyMessage(-50, 575, GetWaveCount()));
			else if(spawnType == 6 || spawnType == 7 )
				MessageSystem::GetInstance()->SendNewMessage(new CCreateRangedEnemyMessage(-5, 575, 2, GetWaveCount()));
			else if(spawnType == 9 || spawnType == 8 )
				MessageSystem::GetInstance()->SendNewMessage(new CCreateRangedEnemyMessage(-50, 575, 3, GetWaveCount()));
		}
		else if(enemyType <= 4)
		{
			if(spawnType <= 6)
				MessageSystem::GetInstance()->SendNewMessage(new CCreateMeleeEnemyMessage(-50, 575, 0, 1));
			else if(spawnType == 8 || spawnType == 7)
				MessageSystem::GetInstance()->SendNewMessage(new CCreateMeleeEnemyMessage(-50, 575, 1, 1));
			else if(spawnType == 9)	
				MessageSystem::GetInstance()->SendNewMessage(new CCreateMeleeEnemyMessage(-50, 575, 2, 1));
		}
	}

	m_nNumOfEnemies++;
}

void CGamePlayState::InitWaves()
{

	m_nMaxEnemies = 0;
	m_nNumOfDeadEnemies = 0;

	TiXmlDocument doc;
	m_vWaves.clear();

	if( doc.LoadFile("resources/XML/Wave.xml") == false)
		return;

	TiXmlElement* Root = doc.RootElement();
	if( Root == NULL )
		return;

	TiXmlElement* pWave = Root->FirstChildElement("Wave");

	do
	{
		double WaveTime;
		int NumOfEnemies;
		pWave->Attribute("time", &WaveTime);
		pWave->Attribute("Enemies", &NumOfEnemies );
		pWave = pWave->NextSiblingElement("Wave");
		WaveInfo tempWave;
		tempWave.fWaveTime = (float)WaveTime;
		tempWave.NumOfEnemies = NumOfEnemies;
		m_vWaves.push_back(tempWave);
	}while(pWave);

	m_bRoundComplete = false;

}

void CGamePlayState::ResetRound()
{
	WaveCount = 0;
	fEnemySpawnBucket = 0.0f;
	m_nNumOfEnemies = 0;
	m_nMaxEnemies = 0;
	m_nNumOfDeadEnemies = 0;
	m_fWaveTime = 0.0f;
	m_bNoTimeUpdate = false;
	m_bRoundComplete = false;
}
bool CGamePlayState::Load( const char* AnimInfo)
{
	int tempID;

	TiXmlDocument doc;

	if(doc.LoadFile(AnimInfo) == false)
		return false;

	TiXmlElement* Root = doc.RootElement();
	if(Root == NULL)
		return false;

	TiXmlElement* NumAnim = Root->FirstChildElement("xNumOfAnim");
	int count = atoi(NumAnim->GetText());

	TiXmlElement* ImageFile = Root->FirstChildElement("xImageFile");
	if(ImageFile)
	{
		tempID = CSGD_TextureManager::GetInstance()->LoadTexture(ImageFile->GetText());
	}

	TiXmlElement* xAnimation  = Root->FirstChildElement("xAnimation");

	while(xAnimation)
	{
		CAnimation a;

		TiXmlElement* NumFrame = xAnimation->FirstChildElement("xNumOfFrames");
		int fcount = atoi(NumFrame->GetText());

		TiXmlElement* looping = xAnimation->FirstChildElement("xLooping");
		a.SetIsLooping((bool)(atoi(looping->GetText())));

		TiXmlElement* AnimSpd = xAnimation->FirstChildElement("xAnimSpd");
		a.SetSpeed((float)( atoi(AnimSpd->GetText())));

		TiXmlElement* xFrame = xAnimation->FirstChildElement("xFrame");

		Frame temp;
		temp.fTime = 0;
		temp.ptAnchor.x = 0;
		temp.ptAnchor.y = 0;

		SetRect(&temp.rAttack,0,0,0,0);
		SetRect(&temp.rCollision,0,0,0,0);
		SetRect(&temp.rRender,0,0,0,0);

		temp.ptAnchor.x = 0;
		temp.ptAnchor.y = 0;


		while(xFrame)
		{
			TiXmlElement* xTime = xFrame->FirstChildElement("xTime");
			if(xTime)
			{
				temp.fTime =(float)( atoi(xTime->GetText()) );
			}
			TiXmlElement* FrameType = xFrame->FirstChildElement("xFrameType");
			if(FrameType)
			{
				TiXmlElement* RenderFrame = FrameType->FirstChildElement("xRenderFrame");
				if(RenderFrame)
				{
					TiXmlElement* RenderFrameX = RenderFrame->FirstChildElement("xRenderFrameX");
					if(RenderFrameX)
						temp.rRender.left =  atoi(RenderFrameX->GetText());

					TiXmlElement* RenderFrameY = RenderFrame->FirstChildElement("xRenderFrameY");
					if(RenderFrameY)
						temp.rRender.top = atoi(RenderFrameY->GetText());

					TiXmlElement* RenderFrameW = RenderFrame->FirstChildElement("xRenderFrameW");
					if(RenderFrameW)
					{
						temp.rRender.right = atoi(RenderFrameW->GetText());
						temp.rRender.right += temp.rRender.left;
					}

					TiXmlElement* RenderFrameH = RenderFrame->FirstChildElement("xRenderFrameH");
					if(RenderFrameH)
					{
						temp.rRender.bottom = atoi(RenderFrameH->GetText());
						temp.rRender.bottom += temp.rRender.top;
					}
				}

				TiXmlElement* CollisionFrame = FrameType->FirstChildElement("xCollisionFrame");
				if(CollisionFrame)
				{
					TiXmlElement* CollisionFrameX = CollisionFrame->FirstChildElement("xCollisionFrameX");
					if(CollisionFrameX)
						temp.rCollision.left = atoi(CollisionFrameX->GetText());

					TiXmlElement* CollisionFrameY = CollisionFrame->FirstChildElement("xCollisionFrameY");
					if(CollisionFrameY)
						temp.rCollision.top = atoi(CollisionFrameY->GetText());

					TiXmlElement* CollisionFrameW = CollisionFrame->FirstChildElement("xCollisionFrameW");
					if(CollisionFrameW)
						temp.rCollision.right = atoi(CollisionFrameW->GetText());

					TiXmlElement* CollisionFrameH = CollisionFrame->FirstChildElement("xCollsionFrameH");
					if(CollisionFrameH)
						temp.rCollision.bottom = atoi(CollisionFrameH->GetText());
				}

				TiXmlElement* AttackFrame = FrameType->FirstChildElement("xAttackFrame");
				if(AttackFrame)
				{
					TiXmlElement* AttackFrameX = AttackFrame->FirstChildElement("xAttackFrameX");
					if(AttackFrameX)
						temp.rAttack.left = atoi(AttackFrameX->GetText());

					TiXmlElement* AttackFrameY = AttackFrame->FirstChildElement("xAttackFrameY");
					if(AttackFrameY)
						temp.rAttack.top = atoi(AttackFrameY->GetText());

					TiXmlElement* AttackFrameW = AttackFrame->FirstChildElement("xAttackFrameW");
					if(AttackFrameW)
						temp.rAttack.right = atoi(AttackFrameW->GetText());

					TiXmlElement* AttackFrameH = AttackFrame->FirstChildElement("xAttackFrameH");
					if(AttackFrameH)
						temp.rAttack.bottom = atoi(AttackFrameH->GetText());
				}

				TiXmlElement* AnchorPoint = FrameType->FirstChildElement("xAnchorPoint");
				if(AnchorPoint)
				{
					TiXmlElement* AnchorPointX = AnchorPoint->FirstChildElement("xAnchorPointX");
					if(AnchorPointX)
					{
						int ax = atoi(AnchorPointX->GetText());
						temp.ptAnchor.x =  ax - temp.rRender.left;
					}

					TiXmlElement* AnchorPointY = AnchorPoint->FirstChildElement("xAnchorPointY");
					if(AnchorPointY)
					{
						int ay = atoi(AnchorPointY->GetText());
						temp.ptAnchor.y = ay - temp.rRender.top;
					}
				}
			}
			a.SetTimeDelay(temp.fTime);

			temp.rAttack.right +=  temp.rAttack.left;
			temp.rAttack.bottom += temp.rAttack.top;
			temp.rAttack.left -= (temp.ptAnchor.x + temp.rRender.left);
			temp.rAttack.top -= (temp.ptAnchor.y + temp.rRender.top);
			temp.rAttack.right -= (temp.ptAnchor.x + temp.rRender.left);
			temp.rAttack.bottom -=(temp.ptAnchor.y + temp.rRender.top);

			temp.rCollision.right +=  temp.rCollision.left;
			temp.rCollision.bottom += temp.rCollision.top;
			temp.rCollision.left -= (temp.ptAnchor.x + temp.rRender.left);
			temp.rCollision.top -= (temp.ptAnchor.y + temp.rRender.top);
			temp.rCollision.right -= (temp.ptAnchor.x + temp.rRender.left);
			temp.rCollision.bottom -=(temp.ptAnchor.y + temp.rRender.top);


			a.addFrame(temp);
			xFrame = xFrame->NextSiblingElement("xFrame");
		}
		a.SetImageID(tempID);
		vAnimation.push_back(a);

		xAnimation = xAnimation->NextSiblingElement("xAnimation");
	}
	return true;
}
